from fastapi import APIRouter, Depends, HTTPException,Query
from sqlalchemy import and_
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
from database import get_db
from schemas.admin_schemas import AdminCreate, AdminUpdate
from services.auth_service import require_user

from config import *  # 导入所有配置
from models.user_models import *

from services.user_service import get_all_users,get_user,create_user,delete_user,update_user

router = APIRouter()

@router.get("/username")
def get_username(user_id: int = Query(..., description="用户ID"),
    user_type_str: str = Query(..., description="用户类型"),
    db: Session = Depends(get_db)):


    # 转换字符串为 Enum
    enum_user_type = UserType.from_str(user_type_str)
    if enum_user_type is None:
        raise HTTPException(status_code=400, detail="无效的用户类型")

    account = db.query(Account).filter(
        and_(Account.user_type == enum_user_type,
        Account.user_ref_id == user_id)
    ).first()

    if not account:
        raise HTTPException(status_code=404, detail="未找到账号")

    return {"username": account.username}
